package vn.edu.ptithcm.pim.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.ParameterizableViewController;

import vn.edu.ptithcm.pim.common.bean.DocumentObject;
import vn.edu.ptithcm.pim.constant.Constants;
import vn.edu.ptithcm.pim.constant.UrlConstants;
import vn.edu.ptithcm.pim.dom.Project;
import vn.edu.ptithcm.pim.dom.Resource;
import vn.edu.ptithcm.pim.dom.ResourceCategory;
import vn.edu.ptithcm.pim.service.ProjectService;
import vn.edu.ptithcm.pim.service.ResourceCategoryService;
import vn.edu.ptithcm.pim.service.ResourceService;
import vn.edu.ptithcm.pim.service.UserService;
import vn.edu.ptithcm.pim.util.ParamUtil;

public class DocumentController extends ParameterizableViewController {
	private UserService userService;
	private ProjectService projectService;
	private ResourceService resourceService;
	private ResourceCategoryService resourceCategoryService;

	Log log = LogFactory.getLog(DocumentController.class);

	@Override
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		int pnum = ParamUtil.getInteger(request, "pnum", 0);
		Map<String, Object> bean = new HashMap<String, Object>();

		if (userService.isSignedIn(request)) {
			Project project = projectService.findByNumber(pnum);
			bean.put("project", project);
			String loginName = userService.getCurrentLogin(request);
			bean.put("user", userService.findByLoginName(loginName));
			bean.put("isSigned", true);
			bean.put("tab", "documents");
			bean.put(Constants.LOGIN_NAME, loginName);
			bean.put(Constants.USER_NAME, userService.getUsername(loginName));
			bean.put("isAdmin", userService.isAdmin(loginName));

			List<Long> resourceCategoryIds = resourceService
					.getAvailableResourceCategoryIds(project.getProjectid());
			List<DocumentObject> documentObjects = new ArrayList<DocumentObject>();
			log.info("resourceCategoryIds's size : "
					+ resourceCategoryIds.size());
			for (Long id : resourceCategoryIds) {
				List<Resource> resources = resourceService.findByProject(
						project.getProjectid(), id);
				ResourceCategory resourceCategory = resourceCategoryService
						.findById(id);
				DocumentObject documentObject = new DocumentObject(
						resourceCategory, resources);
				documentObjects.add(documentObject);
			}
			bean.put("documents", documentObjects);

			request.setAttribute("bean", bean);
		} else {
			String redirectLink = UrlConstants.DOCUMENTS_URL + UrlConstants.HTM
					+ "?pnum=" + pnum;
			request.getSession().setAttribute("redirectLink", redirectLink);
			response.sendRedirect(UrlConstants.HOME_PAGE_URL + UrlConstants.HTM);
		}

		return new ModelAndView(UrlConstants.DOCUMENTS_URL, "bean", bean);
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public ProjectService getProjectService() {
		return projectService;
	}

	public void setProjectService(ProjectService projectService) {
		this.projectService = projectService;
	}

	public ResourceService getResourceService() {
		return resourceService;
	}

	public void setResourceService(ResourceService resourceService) {
		this.resourceService = resourceService;
	}

	public ResourceCategoryService getResourceCategoryService() {
		return resourceCategoryService;
	}

	public void setResourceCategoryService(
			ResourceCategoryService resourceCategoryService) {
		this.resourceCategoryService = resourceCategoryService;
	}

}
